package com.qinggeng.project.api.controller;

import com.qinggeng.common.constant.Constants;
import com.qinggeng.common.enums.*;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.project.api.dto.ResumeDto;
import com.qinggeng.project.api.security.annotation.LoginRequired;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.domain.JobUser;
import com.qinggeng.project.system.domain.Resume;
import com.qinggeng.project.system.service.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.testng.collections.Maps;

import java.util.Map;

/**
 * @author smallfish
 */
@Api(tags = "简历模块")
@RestController
@CrossOrigin
@RequestMapping("${apiPath}/resume")
public class ApiResumeController extends BaseController {

    @Autowired
    private IResumeService resumeService;

    @Autowired
    private IJobUserService jobUserService;

    @Autowired
    private IJobInfoCollectService jobInfoCollectService;
    @Autowired
    private IJobInfoBrowseService jobInfoBrowseService;

    @Autowired
    private IPositionService positionService;
    @Autowired
    private JobDliveryService jobDliveryService;
    @Autowired
    private JobMessageService jobMessageService;



    @ApiOperation("简历列表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "keyword" ,value = "申请职位关键词"),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记",required = true)
    })
    @LoginRequired(must = false)
    public AjaxResult resumeList(Resume resume,String keyword){
        startPage();
        resume.setApplyPosition(keyword);
        return AjaxResult.success(resumeService.listResumeDto(resume));
    }

    @ApiOperation("我的简历")
    @GetMapping("/myResume")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记",required = true)
    })
    @LoginRequired()
    public AjaxResult myResume(){
        Resume resume=resumeService.getResumeByUserId();
        if(null==resume){
            return AjaxResult.success(new Resume());
        }
        return AjaxResult.success(resume);
    }


    @ApiOperation("简历详情")
    @GetMapping("/info")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "Long",dataType = "query",name = "id",value = "简历id",required = true),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记")
    })
    @LoginRequired(must = false)
    public AjaxResult resumeInfo(Long id){
        ResumeDto resumeDto=resumeService.getResumeDtoById(id);
        if(null==resumeDto.getStatus()||!resumeDto.getStatus().equals(CheckStatusEnum.已通过.getValue())){
            return AjaxResult.error("等待审核通过",ShowTypeEnum.用户.getValue());
        }
        //添加浏览记录
        if(ApiUtils.getIsLogin()){
            Long jobUserId=jobUserService.getJobUserId();
            jobInfoBrowseService.updateJobInfoBrowse(jobUserId,id,JobInfoTypeEnum.RESUME_INFO.getType());
            resumeDto.setIsCollect(jobInfoCollectService.checkJobInfoIsCollect(id,JobInfoTypeEnum.RESUME_INFO.getType()));
            resumeDto.setIsDelivery(jobDliveryService.checkResumeIsInterested(jobUserId,resumeDto.getJobUserId()));
        }
        return AjaxResult.success(resumeDto);

    }

    @ApiOperation("简历浏览记录")
    @GetMapping("/browse")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "Long",dataType = "query",name = "id",value = "简历id",required = true),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记")
    })
    @LoginRequired(must = false)
    public AjaxResult browseRecord(Long id){
        Map<String,Object> data= Maps.newHashMap();
        data.put("count",resumeService.getResumeBrowseCountById(id));
        data.put("list",jobInfoBrowseService.listJobInfoBrowseUserByJobInfoIdAndBrowseType(id,JobInfoTypeEnum.RESUME_INFO.getType()));
        return AjaxResult.success(data);
    }

    @ApiOperation("我的浏览记录")
    @GetMapping("/myBrowse")
    @ApiImplicitParams({
          @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记",required = true)
    })
    @LoginRequired()
    public AjaxResult myBrowseRecord(){
        return AjaxResult.success(resumeService.listBrowseResumeDto());
    }




    @ApiOperation("简历收藏")
    @GetMapping("/collect")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "Long",dataType = "query",name = "id",value = "简历id",required = true),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记",required = true)
    })
    @LoginRequired()
    public AjaxResult resumeCollect(Long id){
        resumeService.getResumeDtoById(id);
        Long jobUserId=jobUserService.getJobUserId();
        return toAjax(jobInfoCollectService.updateJobInfoCollect(jobUserId,id, JobInfoTypeEnum.RESUME_INFO.getType()));
    }

    @ApiOperation("收藏列表")
    @GetMapping("/collectList")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记",required = true)
    })
    @LoginRequired()
    public AjaxResult collectList(){       ;
        return AjaxResult.success( resumeService.listCollectResumeDto());
    }

    @ApiOperation("简历编辑")
    @PostMapping("/edit")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记",required = true)
    })
    @LoginRequired()
    public AjaxResult addResume(Resume resume){
        Long jobUserId=jobUserService.getJobUserId();
        //发布简历
        if(null==resume.getId()){
            resume.setJobUserId(jobUserId);            ;
            return toAjax(resumeService.insertResume(resume));
        }
        //修改简历
        if(null==resume.getJobUserId()||resume.getJobUserId().compareTo(jobUserId)!=0){
            return AjaxResult.error("没有权限修改！",ShowTypeEnum.用户.getValue());
        }
        return toAjax(resumeService.updateResume(resume));
    }

    @ApiOperation("简历删除")
    @PostMapping("/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "Long",dataType = "query",name = "id",value = "简历id",required = true),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记",required = true)
    })
    @LoginRequired()
    public AjaxResult deleteResume(Long id){
        return toAjax(resumeService.deleteResumeById(id));
    }


    @ApiOperation("获取用户信息")
    @GetMapping("/getUserInfo")
    @ApiImplicitParams({
           @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记",required = true)
    })
    @LoginRequired
    public AjaxResult getUserInfo(){
        JobUser jobUser=jobUserService.getJobUserByUserId(ApiUtils.getUserId());
        return AjaxResult.success(jobUser);
    }

    @ApiOperation("添加用户信息")
    @PostMapping("/addUserInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记",required = true)
    })
    @LoginRequired
    public AjaxResult addUserInfo(JobUser jobUser){        ;
        return toAjax(jobUserService.addJobUser(jobUser));
    }


    @ApiOperation("统计数量")
    @GetMapping("/count")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记",required = true)
    })
    @LoginRequired()
    public AjaxResult count(){
        Map<String,Object> data=Maps.newHashMap();
        Resume resume=resumeService.getResumeByUserId();
        data.put("browseCount",resume!=null?resumeService.getResumeBrowseCountById(resume.getId()):0);
        data.put("collectCount",positionService.getPositionCollectCountByUserId());
        data.put("deliveryCount",jobDliveryService.getJobDeliveryCountByResumeUserId());
        data.put("messageCount",jobMessageService.getJobMessageCountByResumeUserId());
        return AjaxResult.success(data);
    }

}
